package md.mru.core.repositories;

import md.mru.core.domains.OrderType;
import md.mru.core.domains.OrderTypeGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;

/**
 * @author sergiu.marin
 * @since 01.04.2014
 */
public interface OrderTypeRepository extends JpaRepository<OrderType, Long> {

    @Query("SELECT DISTINCT o FROM OrderType o LEFT JOIN FETCH o.orderPatterns WHERE o.orderTypeGroup.id=:groupId")
    List<OrderType> loadOrderTypeByGroup(@Param(value = "groupId") long groupId);

    @Query("SELECT o FROM OrderType o WHERE o.id=:typeId")
    OrderType loadOrderType(@Param(value = "typeId") long typeId);

    @Query("SELECT o FROM OrderType o WHERE o.orderTypeGroup.id IN :ids")
    List<OrderType> loadOrderTypesByGroups(@Param(value = "ids") Set<Long> ids);

    /*@Modifying
    @Query("UPDATE OrderType o SET o.orderTemplate=:orderTemplate WHERE o.id=:orderTypeId")
    void save(@Param(value = "orderTypeId") long orderTypeId, @Param(value = "orderTemplate") String orderTemplate);*/
}
